package jdbc;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class adduser
 */
@WebServlet("/src/adduser")
public class adduser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public adduser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
	    String user = request.getParameter("user");
	    String name = request.getParameter("name");
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    hello a = new hello();//模板
		try {//模板
			if(user.equals("student")) {
				a.doupdate("INSERT INTO student (student_name,student_username,student_password) VALUES (?, ?, ?) ",name,username,password);
				response.getWriter().write("true");
			}
			else if(user.equals("teacher")) {
				a.doupdate("INSERT INTO teacher (teacher_name,teacher_username,teacher_password) VALUES (?, ?, ?) ",name,username,password);
				response.getWriter().write("true");
			}
			else {
				a.doupdate("INSERT INTO secretary (secretary_name,secretary_username,secretary_password) VALUES (?, ?, ?) ",name,username,password);
				response.getWriter().write("true");
			}
		
		} catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板		
	}

}
